package com.qs.user.ui.VM;

import android.app.Application;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableBoolean;
import android.databinding.ObservableField;
import android.databinding.ObservableList;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.view.View;

import com.qs.base.data.MyFileEntity;
import com.qs.base.utils.FileUtil;
import com.qs.user.BR;
import com.qs.user.R;
import com.qs.user.databinding.UserMyFileItemBinding;
import com.qs.user.pay.util.OpenFileUtil;
import com.qs.user.ui.activity.MyFileFragment;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
import me.tatarka.bindingcollectionadapter2.OnItemBind;

/**
 * 我的文件VM
 *
 * @Author ltzz
 * @Date 2019/8/24
 */
public class MyFileViewModel extends BaseViewModel {
    public MyFileViewModel(@NonNull Application application) {
        super(application);
    }

    //文件目录
    public ObservableField<String> pathStr = new ObservableField<>(FileUtil.bPath + "/out/");
    //type,0全部，1txt，2word
    public int type;
    //我的文件数据
    public List<MyFileEntity> myFiles = new ArrayList<>();
    //是否点击多选
    public Boolean isSelect = false;
    //当前显示的fragment
    public int showSelect = 0;
    //选中的文件
    public Set<MyFileItemViewModel> itemx = new HashSet<>();

    //各个fragment
    public MyFileFragment fileFragment1;
    public MyFileFragment fileFragment2;
    public MyFileFragment fileFragment3;
    //type
    public int typeF = 0;

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //多选按钮点击
        public ObservableBoolean moreSelectUc = new ObservableBoolean(false);
    }


    //多选点击事件
    public BindingCommand moreSelectClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            isSelect = !isSelect;
            uc.moreSelectUc.set(!uc.moreSelectUc.get());
        }
    });
    //删除点击事件
    public BindingCommand deleteClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            switch (typeF) {
                case 0:
                    fileFragment1.delete();
                    break;
                case 1:
                    fileFragment2.delete();
                    break;
                case 2:
                    fileFragment3.delete();
                    break;
            }
        }
    });

    //给RecyclerView添加ObservableList
    public ObservableList<MyFileItemViewModel> observableList = new ObservableArrayList<>();
    //RecyclerView多布局添加ItemBinding
    public ItemBinding<MyFileItemViewModel> itemBinding = ItemBinding.of(new OnItemBind<MyFileItemViewModel>() {
        @Override
        public void onItemBind(ItemBinding itemBinding, int position, MyFileItemViewModel item) {
            //设置布局
            itemBinding.set(BR.viewModel, R.layout.user_my_file_item);
        }
    });
    //给RecyclerView添加Adpter，请使用自定义的Adapter继承BindingRecyclerViewAdapter，重写onBindBinding方法，里面有你要的Item对应的binding对象
    public BindingRecyclerViewAdapter<MyFileItemViewModel> adapter = new BindingRecyclerViewAdapter<MyFileItemViewModel>() {
        @Override
        public void onBindBinding(ViewDataBinding binding, int variableId, int layoutRes, int position, final MyFileItemViewModel item) {
            super.onBindBinding(binding, variableId, layoutRes, position, item);
            final UserMyFileItemBinding mBD = (UserMyFileItemBinding) binding;
            if (StringUtils.equals(item.mItemEntity.get().getFileType(), "txt"))
                mBD.fileIcon.setImageResource(R.drawable.user_txt_icon);
            else
                mBD.fileIcon.setImageResource(R.drawable.user_doc_icon);
            if (item.isSelect.get() == View.VISIBLE) {
                mBD.getRoot().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (itemx.add(item)) {
                            mBD.select.setSelected(true);
                        } else {
                            itemx.remove(item);
                            mBD.select.setSelected(false);
                        }
                    }
                });
            } else {
                mBD.getRoot().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        File file = new File(pathStr.get().toString() + item.mItemEntity.get().getFileName());
                        OpenFileUtil.openFileByPath(getApplication(), file.getPath());

                    }
                });
            }
        }
    };
}
